package com.tuling.vipes.controller;

import com.tuling.vipes.bean.Product;
import com.tuling.vipes.service.impl.SearchService;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 基于es的官方sdk封装的接口
 * @author maguodong
 * @
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/query")
    public List<Product> search(@RequestParam String keyword) throws IOException {
        return searchService.search(keyword);
    }

    @GetMapping("/nested")
    public List<Product> nestedSearch(@RequestParam String category) throws IOException {
        return searchService.nestedSearch(category);
    }

    @GetMapping("/range")
    public List<Product> rangeSearch(@RequestParam double minPrice, @RequestParam double maxPrice) throws IOException {
        return searchService.rangeSearch(minPrice, maxPrice);
    }

    @GetMapping("/aggregate")
    public void aggregateSearch() throws IOException {
        searchService.aggregateSearch();
    }

    @GetMapping("/sort")
    public List<Product> sortSearch(@RequestParam String field, @RequestParam String order) throws IOException {
        return searchService.sortSearch(field, SortOrder.fromString(order));
    }

    @GetMapping("/geo")
    public List<Product> geoDistanceSearch(@RequestParam double lat, @RequestParam double lon, @RequestParam String distance) throws IOException {
        return searchService.geoDistanceSearch(lat, lon, distance);
    }

    @GetMapping("/nested-detail")
    public List<Product> nestedDetailSearch(@RequestParam String key, @RequestParam String value) throws IOException {
        return searchService.nestedDetailSearch(key, value);
    }
}